<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="artDialog/skins/default.css" type="text/css"></link>
<link href="css/select.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#user_add_tb input{background-color:#eeeeee;width:120px;}
#user_add_tb label{width:86px;line-height:34px; display:block;color:#7f7f7f;padding-right:20px;}
#user_add_tb td{padding-top:8px;}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/jquery.artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.plugins.min.js"></script>
<script type="text/javascript" src="js/util.js"></script>

<script type="text/javascript">
$(function(){
	show_user_list(1)
});

function show_user_list(pageIndex){
	$.post("user!findCurrentUser.action",function(data){
		var cuser = eval("("+data+")");
	$.post("user!findList.action",{"pageIndex":pageIndex},function(data){
		
		var bean = eval("("+data+")");
		var users = bean.rows;
		var html = "";
		for(var i = 0 ; i < users.length;i++){
		
				var operation = "<td><a href=\"javascript:void(0)\"  onclick='user_purview("+users[i].id+",\""+users[i].name+"\","+users[i].group.id+")'> 权限   </a><a href=\"javascript:void(0)\" onclick='user_update("+users[i].id+",\""+users[i].name+"\","+users[i].state+")' >修改</a>     <a href=\"javascript:void(0)\" onclick='users_del("+users[i].id+")' >删除</a></td>";
				if(cuser.id==users[i].id || users[i].group.id==1){
					operation = "<td>不可操作</td>";
				}
				html+="<tr><td>"+users[i].id+"</td><td>"+users[i].username+"</td><td>"+users[i].name+"</td><td>"+users[i].group.name+"</td><td>"+users[i].joindate+"</td><td>"+(users[i].state==1 ? "正常" : "已冻结")+"</td>"+operation+"</tr>";
		}
     	var pagerHTML=pager(bean,"show_user_list");
      	$(".pagin").html(pagerHTML);
			$("#user_list").html(html);
			$('#user_list tr:odd').addClass('odd');
	});
	});
}

function users_del(id){
	okCancelAlert("删除用户","确定删除此用户?",function(){
		$.post("user!deleteUser.action",{uid:id},function(data){
			var bean = eval("("+data+")");
			
			if(bean.code==1){	
				location.href="user.html";
				
			}else if(bean.code==0){
				defaultAlert("删除用户","删除失败");
			}else{
				defaultAlert("删除用户","未知错误");
			}
		});
	},function(){});
	
	}



	

function user_purview(id,name,group){
	var selectHtml = "<table><tr><td style='padding-bottom:20px;'> 权限:</td><td><select class=\"stateSelect\" id='stateSelect'>";
	$.post("group!findGroupList.action",function(data){
		var groups = eval("("+data+")");
		for(var i =0;i<groups.length;i++){
			selectHtml+="<option value='"+groups[i].id+"'>"+groups[i].name+"</option>";
		}
		selectHtml+="</select></td></tr></table>";
		var html =""+selectHtml;
		art.dialog({title:"修改权限-"+name,content:html,lock:true,fixed:true,okValue:"修改",cancelValue:"取消",ok:function(){
			var ugroup = $(".stateSelect").val();
			if(ugroup!=group){
				$.post("user!updateUsergroup.action",{"uid":id,"groupid" : ugroup},function(data){
					var json = eval("("+data+")");
					if(json.code==1){
						okCancelAlert("修改权限","修改成功",function(){location.href="user.html";},function(){location.href="user.html";});
					}else{
						okCancelAlert("修改权限","修改失败",function(){location.href="user.html";},function(){location.href="user.html";});
					}
				});
			}
		},cancel:function(){}});
		$(".stateSelect").uedSelect({width:100});
	});
	//selectHtml+="<option value='1' "+(group==1 ? "selected='selected'" : "")+">系统管理员</option>";
	//selectHtml+="<option value='2' "+(group==2 ? "selected='selected'" : "")+">线路管理员</option>";
	//selectHtml+="<option value='3' "+(group==3 ? "selected='selected'" : "")+">巡检员</option>";
	//selectHtml+="<option value='4'"+(group==4 ? "selected='selected'" : "")+">消缺员</option></select></td></tr></table>";
	
	//art.dialog({title:"修改权限-"+name,content:html,lock:true,fixed:true,okValue:"修改",cancelValue:"取消",ok:function(){
		//var ugroup = $(".stateSelect").val();
		//if(ugroup!=group){
			//$.post("user!updateUsergroup.action",{"uid":id,"groupid" : ugroup},function(data){
				//var json = eval("("+data+")");
				//if(json.code==1){
					//okCancelAlert("修改权限","修改成功",function(){location.href="user.html";},function(){location.href="user.html";});
				//}else{
					//okCancelAlert("修改权限","修改失败",function(){location.href="user.html";},function(){location.href="user.html";});
				//}
			//});
		//}
	//},cancel:function(){}});
	//$(".stateSelect").uedSelect({width:100});
}











function user_update(id,name,state){
	var selectHtml = "<table><tr><td style='padding-bottom:5px;'>状态:</td><td><select class=\"stateSelect\" id='stateSelect'>";
	selectHtml+="<option value='0' "+(state==0 ? "selected='selected'" : "")+">冻结</option>";
	selectHtml+="<option value='1'"+(state==1 ? "selected='selected'" : "")+">正常</option></select></td></tr></table>";
	var html =""+selectHtml;
	art.dialog({title:"修改状态-"+name,content:html,lock:true,fixed:true,okValue:"修改",cancelValue:"取消",ok:function(){
		var ustate = $(".stateSelect").val();
		if(ustate!=state){
			$.post("user!updateUserState.action",{"uid":id,"state" : ustate},function(data){
				var json = eval("("+data+")");
				if(json.code==1){
					
					okCancelAlert("修改状态","修改成功",function(){location.href="user.html";},function(){location.href="user.html";});
					
				}else{
					okCancelAlert("修改状态","修改失败",function(){location.href="user.html";},function(){location.href="user.html";});
				}
			});
		}
	},cancel:function(){}});
	$(".stateSelect").uedSelect({width:80});
}

function user_add(){
	$.post("group!findGroupList.action",function(data){
		var groups = eval("("+data+")");
		var selectHtml=" <select class=\"groupSelect\" id='groupSelect'>";
		for(var i =0;i<groups.length;i++){
			selectHtml+="<option value='"+groups[i].id+"'>"+groups[i].name+"</option>";
		}
		selectHtml+="</select>";
		var html = "<table id='user_add_tb'><tr><td align='right'><label>登录账号:</label></td><td><input type='text' class='dfinput' id='add_user_account'/></td></tr><tr><td align='right'><label>用户名称:</label></td><td><input type='text' id='add_user_name' class='dfinput'/></td></tr><tr><td align='right'><label>密码:</label></td><td><input class='dfinput' type='password' id='add_user_password'/></td></tr><tr><td align='right'><label>用户组:</label></td><td >"+selectHtml+"</td></tr></table>";
		art.dialog({title:"添加用户",content:html,lock:true,fixed:true,okValue:"添加",cancelValue:"取消",ok:function(){
			var username = document.getElementById("add_user_account");
			var name = document.getElementById("add_user_name");
			var group = document.getElementById("groupSelect");
			var password = document.getElementById("add_user_password");
			if(username.value==""){
				username.select();
				username.focus();
				this.shake();
				return false;
			}
			if(name.value==""){
				name.select();
				name.focus();
				this.shake();
				return false;
			}
			if(group.value==""){
				group.select();
				group.focus();
				this.shake();
				return false;
			}
			if(password.value==""){
				password.select();
				password.focus();
				this.shake();
				return false;
			}
			$.post("user!addUser.action",{"user.username":username.value,"user.password":password.value,"user.name":name.value,"user.group.id":group.value},function(data){
				var json = eval("("+data+")");
				if(json.code==1){
					defaultAlert("添加用户","添加成功");
					location.href="user.html";
				}else{
					defaultAlert("添加用户","添加失败");
				}
			});
			
		},cancel:function(){}});
		$(".groupSelect").uedSelect({width:100});
	});
}

</script>


</head>


<body>

	<div class="place">
    <span>位置：</span>
    <ul class="placeul">
    <li><a href="#">首页</a></li>
    <li><a href="#">系统管理</a></li>
    <li><a href="#">用户管理</a></li>
    </ul>
    </div>
    
    <div class="rightinfo">
    
    <div class="tools">
    
    	<ul class="toolbar">
        <li onclick="user_add()"><span><img src="images/t01.png" /></span>添加</li>
        </ul>
    </div>
    
    
    <table class="tablelist">
    	<thead>
    	<tr>
        <th>编号</th>
        <th>用户账号</th>
        <th>用户名称</th>
        <th>用户组</th>
        <th>入职时间</th>
        <th>状态</th>
        <th>操作</th>
        </tr>
        </thead>
        <tbody id="user_list">
       
        </tbody>
    </table>
    <div  class="pagin"></div>
   
   
    
    

    <script type="text/javascript">
	$('.tablelist tbody tr:odd').addClass('odd');
	</script>

</body>
</html>
